<template>
  <el-dialog :title="(type == 'add')? '门店仓位配置新增' : '门店仓位配置更新'" :close-on-click-modal="false" fullscreen :visible.sync="visible" center>
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form ref="dataForm" inline>
      <el-form-item label="选择门店">
        <el-select v-model="deptId" @change="deptSelectChange" :disabled="this.type == 'update'">
          <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-s-tools" @click="recommendSetting">推荐配置</el-button>
      </el-form-item>
      <el-table :data="ctgSaleList" border>
        <el-table-column prop="name" label="商品类别"></el-table-column>
        <el-table-column prop="saleStockName" label="销售仓位">
          <template slot-scope="scope">
              <el-select v-model="scope.row.saleStockId">
                <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="refundStockName" label="退款仓位">
          <template slot-scope="scope">
            <el-select v-model="scope.row.refundStockId">
              <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getDeptInfo} from '@/api/admin/dept'
  import { getStockList } from '@/api/eims/stock'
  import {
    fetchCtgSaleList,
    fetchStockConfigByDeptId,
    getStockConfigObj,
    addStockConfigObj,
    putStockConfigObj
  } from '@/api/eims/stock-config'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        id: '',
        deptId: '',
        deptName: '',
        type: '',
        deptOptions: [],
        ctgSaleList: [],
        stockOptions: [],
        dataList: [],
        dataForm: {}
      }
    },
    methods: {
      init(data) {
        this.type = data.type
        if(this.type == 'update'){
          this.deptId = data.deptId
          this.deptName = data.deptName
        }
        this.visible = true
        this.$nextTick(() => {
          this.getDeptList()
          this.getCtgSaleList()
          this.getAllStockList()
        })
      },
      // 部门change事件
      deptSelectChange(val) {
        this.deptOptions.find(item => {
          if(item.id == val){
            this.deptName = item.name
          }
        })
      },
      // 推荐部门
      recommendSetting() {
        let params = {
          deptId: this.deptId
        }
        getStockList(params).then(response => {
          if(response.data.code == '0'){
            if(response.data.data.length){
              let recommendStockId = response.data.data[0].id
              let recommendStockName = response.data.data[0].name
              this.ctgSaleList.forEach(item => {
                item.deptId = this.deptId
                item.saleStockId = recommendStockId
                item.refundStockId = recommendStockId
                if(item.type == '2'){
                  item.clearProcessId = recommendStockId
                }
              })
            }
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        let list = []
        this.ctgSaleList.forEach(item => {
          let obj = {
            type: item.type,
            deptId:this.deptId,
            deptName: this.deptName,
            ctgId:item.ctgId,
            ctgSaleId: item.id,
            ctgSaleName: item.name,
            saleStockId:item.saleStockId,
            refundStockId: item.refundStockId,
          }
          if(item.type == '2'){
            obj.clearProcessId = item.clearProcessId
          }
          if(this.type == 'update'){
            obj.id = item.id
            obj.ctgSaleId = item.ctgSaleId
            obj.ctgSaleName = item.ctgSaleName
          }
          list.push(obj)
        })
        this.dataForm.list = list
        // 判断是否有未填项
        for(let i = 0; i < this.dataForm.list.length; i++){
          if(!this.dataForm.list[i].saleStockId){
            this.$message.warning(this.dataForm.list[i].ctgSaleName + '销售仓位不能为空')
            return false
          }
          if(!this.dataForm.list[i].refundStockId){
            this.$message.warning(this.dataForm.list[i].ctgSaleName + '退款仓位不能为空')
            return false
          }
        }
        this.ischeck = true
        if (this.type == 'update') {
          this.dataForm.id = this.id
          putStockConfigObj(this.dataForm.list).then(response => {
            if(response.data.code == '0'){
              this.$message.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }else{
              this.$message.error(response.data.msg)
            }
            this.ischeck = false
          }).catch(() => {
                this.ischeck = false
              })
        } else {
          addStockConfigObj(this.dataForm.list).then(response => {
            if(response.data.code == '0'){
              this.$message.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }else{
              this.$message.error(response.data.msg)
            }
            this.ischeck = false
          }).catch(() => {
                this.ischeck = false
              })
        }
      },
      // 获取门店列表
      getDeptList(){
        getDeptInfo().then(response => {
          if(response.data.code == '0'){
            this.deptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 获取商品销售类别列表
      getCtgSaleList(){
        fetchCtgSaleList({type:3}).then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data
            resultData.forEach(item => {
              item.saleStockId = ''
              item.refundStockId = ''
              if(item.type == 2){
                item.clearProcessId = ''
              }
            })
            this.ctgSaleList = resultData
            this.$nextTick(() => {
              if (this.type == 'update') {
                fetchStockConfigByDeptId({ deptId: this.deptId }).then(result => {
                  if(result.data.code == '0'){
                    let resultData1 = result.data.data
                    this.ctgSaleList.forEach((item,index) => {
                      // 重新赋值
                      item.id = resultData1[index].id
                      item.ctgSaleId = resultData1[index].ctgSaleId
                      item.ctgSaleName = resultData1[index].ctgSaleName
                      item.saleStockId = resultData1[index].saleStockId
                      item.refundStockId = resultData1[index].refundStockId
                      if(item.type == 2){
                        item.clearProcessId = resultData1[index].clearProcessId
                      }
                    })
                  }else{
                    this.$message.error(result.data.msg)
                  }
                })
              }
            })
          }else{
            this.$message.error(response.data.msg)
          }

        })
      },
      getAllStockList() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>
